//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "NumericHaganPricer.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Quote.h>
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Cashflows/HaganPricer.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CNumericHaganPricer::CNumericHaganPricer (boost::shared_ptr<QuantLib::NumericHaganPricer>& childNative, Object^ owner) : CHaganPricer(CNumericHaganPricer::typeid)
{
#ifdef HANDLE
	_phNumericHaganPricer = NULL;
#endif
	_ppNumericHaganPricer = &childNative;
    _ppHaganPricer = new boost::shared_ptr<QuantLib::HaganPricer> (boost::dynamic_pointer_cast<QuantLib::HaganPricer> (*_ppNumericHaganPricer));
}
Cephei::QL::Cashflows::CNumericHaganPricer::CNumericHaganPricer (QuantLib::NumericHaganPricer& childNative, Object^ owner) : CHaganPricer(CNumericHaganPricer::typeid)
{
#ifdef HANDLE
	_phNumericHaganPricer = NULL;
#endif
	_ppNumericHaganPricer = new boost::shared_ptr<QuantLib::NumericHaganPricer> (&childNative);
    _ppHaganPricer = new boost::shared_ptr<QuantLib::HaganPricer> (boost::dynamic_pointer_cast<QuantLib::HaganPricer> (*_ppNumericHaganPricer));
    _NumericHaganPricerOwner = owner;
    _HaganPricerOwner = owner;
}

Cephei::QL::Cashflows::CNumericHaganPricer::CNumericHaganPricer (CNumericHaganPricer^ copy) : CHaganPricer(CNumericHaganPricer::typeid)
{
#ifdef HANDLE
	_phNumericHaganPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppNumericHaganPricer = new boost::shared_ptr<QuantLib::NumericHaganPricer> (copy->GetShared());
        _ppHaganPricer = new boost::shared_ptr<QuantLib::HaganPricer> (boost::dynamic_pointer_cast<QuantLib::HaganPricer> (*_ppNumericHaganPricer));
    }
}
Cephei::QL::Cashflows::CNumericHaganPricer::CNumericHaganPricer (System::Type^ t) : CHaganPricer(CNumericHaganPricer::typeid)
{
#ifdef HANDLE
	_phNumericHaganPricer = NULL;
#endif
	if (!t->IsSubclassOf(CNumericHaganPricer::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CNumericHaganPricer::CNumericHaganPricer (QuantLib::Handle<QuantLib::NumericHaganPricer>& childNative, Object^ owner)  : CHaganPricer(CNumericHaganPricer::typeid)
{
	_phNumericHaganPricer = &childNative;
	_ppNumericHaganPricer = &static_cast<boost::shared_ptr<QuantLib::NumericHaganPricer>>(childNative.currentLink());
    _ppHaganPricer = new boost::shared_ptr<QuantLib::HaganPricer> (boost::dynamic_pointer_cast<QuantLib::HaganPricer> (*_ppNumericHaganPricer));
    _NumericHaganPricerOwner = owner;
}
Cephei::QL::Cashflows::CNumericHaganPricer::CNumericHaganPricer (QuantLib::Handle<QuantLib::NumericHaganPricer> childNative)  : CHaganPricer(CNumericHaganPricer::typeid)
{
	_phNumericHaganPricer = &childNative;
	_ppNumericHaganPricer = &static_cast<boost::shared_ptr<QuantLib::NumericHaganPricer>>(childNative.currentLink());
    _ppHaganPricer = new boost::shared_ptr<QuantLib::HaganPricer> (boost::dynamic_pointer_cast<QuantLib::HaganPricer> (*_ppNumericHaganPricer));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CNumericHaganPricer::CNumericHaganPricer (QuantLib::NumericHaganPricer childNative)  : CHaganPricer(CNumericHaganPricer::typeid)
{
#ifdef HANDLE
	_phNumericHaganPricer = NULL;
#endif
	_ppNumericHaganPricer = new boost::shared_ptr<QuantLib::NumericHaganPricer> (new QuantLib::NumericHaganPricer (childNative));
    _ppHaganPricer = new boost::shared_ptr<QuantLib::HaganPricer> (boost::dynamic_pointer_cast<QuantLib::HaganPricer> (*_ppNumericHaganPricer));
}
#endif

Cephei::QL::Cashflows::CNumericHaganPricer::~CNumericHaganPricer ()
{
    if (_ppNumericHaganPricer != NULL)
    {
	    delete _ppNumericHaganPricer;
        _ppNumericHaganPricer = NULL;
    }
}
Cephei::QL::Cashflows::CNumericHaganPricer::!CNumericHaganPricer ()
{
    if (_ppNumericHaganPricer != NULL)
    {
	    delete _ppNumericHaganPricer;
    }
}
QuantLib::NumericHaganPricer& Cephei::QL::Cashflows::CNumericHaganPricer::GetReference ()
{
    if (_ppNumericHaganPricer == NULL) throw gcnew NativeNullException ();
	return **_ppNumericHaganPricer;
}
boost::shared_ptr<QuantLib::NumericHaganPricer>& Cephei::QL::Cashflows::CNumericHaganPricer::GetShared ()
{
    if (_ppNumericHaganPricer == NULL) throw gcnew NativeNullException ();
	return *_ppNumericHaganPricer;
}
QuantLib::NumericHaganPricer* Cephei::QL::Cashflows::CNumericHaganPricer::GetPointer ()
{
    if (_ppNumericHaganPricer == NULL) throw gcnew NativeNullException ();
	return &**_ppNumericHaganPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::NumericHaganPricer>& Cephei::QL::Cashflows::CNumericHaganPricer::GetHandle ()
{
	if (_phNumericHaganPricer == NULL)
	{
		_phNumericHaganPricer = new Handle<QuantLib::NumericHaganPricer> (*_ppNumericHaganPricer);
	}
	return *_phNumericHaganPricer;
}
#endif
bool Cephei::QL::Cashflows::CNumericHaganPricer::HasNative () 
{
	return (_ppNumericHaganPricer != NULL);
}

Double Cephei::QL::Cashflows::CNumericHaganPricer::OptionletPrice (QL::Option::TypeEnum optionType, Double strike)
{
    try
    {
        QuantLib::Option::Type _optionType = (QuantLib::Option::Type)optionType ;
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppNumericHaganPricer)->optionletPrice ( _optionType,  _strike );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CNumericHaganPricer::ResetUpperLimit (Double stdDeviationsForUpperLimit)
{
    try
    {
        QuantLib::Real _stdDeviationsForUpperLimit = (QuantLib::Real)ValueHelper::Convert (stdDeviationsForUpperLimit);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppNumericHaganPricer)->resetUpperLimit ( _stdDeviationsForUpperLimit );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CNumericHaganPricer::StdDeviations::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppNumericHaganPricer)->stdDeviations ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CNumericHaganPricer::SwapletPrice::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppNumericHaganPricer)->swapletPrice ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CNumericHaganPricer::UpperLimit::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppNumericHaganPricer)->upperLimit ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

